﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading.Tasks;

namespace CADAutoPlot
{
    class Preview
    {
        private Bitmap img;
        private Graphics graph;
        private Pen cadPen;

        public Preview(int width,int height)
        {
            img = new Bitmap(width, height);
            graph = Graphics.FromImage(img);
            cadPen = new Pen(Color.Black);
            //graph.DrawImage(img, 0, 0);
            //设置平滑模式
            graph.SmoothingMode = SmoothingMode.HighQuality;
            //graph.TranslateTransform(0, img.Height);

            //更改坐标系

            graph.ScaleTransform(1, -1);
            graph.TranslateTransform(img.Width / 10, -img.Height / 5);

            graph.Clear(Color.White);

        }
        public void AddPolyLine(double[] polyObjects)
        {
            int count = 0;
            double[] polyObject = polyObjects;

            PointF[] polyPts = new PointF[polyObject.Length / 3 + 1]; 
            for(int i = 0 ;i < polyObject.Length;)
            {
                polyPts[count].X = Convert.ToSingle(polyObject[i]);
                polyPts[count].Y = Convert.ToSingle(polyObject[i + 1]);
                i += 3;
                ++count;
            }
            polyPts[polyObject.Length / 3].X = polyPts[0].X;
            polyPts[polyObject.Length / 3].Y = polyPts[0].Y;
            graph.DrawPolygon(cadPen,polyPts);
        }
        public void AddText(string text,double[] startPoint)
        {
            var dstRect = new Rectangle(Convert.ToInt32(startPoint[0]), Convert.ToInt32(startPoint[1]),50,-50);
            var container = graph.BeginContainer(dstRect, new Rectangle(Convert.ToInt32(startPoint[0]), Convert.ToInt32(startPoint[1]), 50, 50), GraphicsUnit.Pixel);
            graph.DrawString(text, new Font("宋体", 20.5F),Brushes.Black,new PointF(Convert.ToSingle(startPoint[0]),Convert.ToSingle(startPoint[1])));
            graph.EndContainer(container);
        }
        public void AddLine(double[] startPoint,double[] endPoint)
        {
            graph.DrawLine(cadPen, new PointF(Convert.ToSingle(startPoint[0]), Convert.ToSingle(startPoint[1])), new PointF(Convert.ToSingle(endPoint[0]), Convert.ToSingle(endPoint[1])));
        }
        public void AddEllipse(double[] startPoint, double[] endPoint,double[] centerPoint,double width,double height,double angle)
        {
            PointF center = new PointF(Convert.ToSingle(centerPoint[0]),Convert.ToSingle(centerPoint[1]));
            PointF leftTop = new PointF(Convert.ToSingle(startPoint[0]), Convert.ToSingle(startPoint[1]));
            //graph.DrawLine(cadPen, new PointF(0, 0), center);
            float boxwidth = Convert.ToSingle(width);
            float boxheight = Convert.ToSingle(height);
            GraphicsState currState = graph.Save();

            graph.TranslateTransform(center.X + boxheight, center.Y);
            graph.RotateTransform(90 - Convert.ToSingle(angle));
            

            graph.DrawEllipse(cadPen, -leftTop.X, leftTop.Y, boxwidth, boxheight);

            graph.Restore(currState);
            //graph.ResetTransform();

        }
        public void saveImg(string path)
        {
           img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

    }
}
